package com.xuecheng.media.api;

import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author Tang
 * @version 1.0
 * @date 2024/1/6 13:13
 */
@Slf4j
@RestController
@RequestMapping("/open")
public class MediaOpenController {
    private final MediaFileService mediaFileService;
    public MediaOpenController(MediaFileService mediaFileService) {
        this.mediaFileService = mediaFileService;
    }

    @GetMapping("/preview/{mediaId}")
    public RestResponse<String> getVideo(@PathVariable("mediaId") String mediaId) {
        MediaFiles file = mediaFileService.getVideoFileById(mediaId);
        if (Objects.isNull(file) || StringUtils.isEmpty(file.getUrl())) {
            return RestResponse.validfail("视频正在转码中....");
        }
        return RestResponse.success(file.getUrl());
    }
}
